package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.symone.webcms.model.Role;
import com.symone.webcms.model.base.ResultMessage;
import com.symone.webcms.model.vo.RoleFormVO;
import com.symone.webcms.model.vo.RoleVO;
import com.symone.webcms.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("api/role")
@Api(value = "RoleController",description = "系统角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequiresPermissions("sys:role:index")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResultMessage list(){
        List<Role> roles = roleService.selectList(new EntityWrapper<Role>().eq("deleted",0));
        return ResultMessage.success("roles",roles);
    }

    @RequiresPermissions("sys:role:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultMessage add(RoleFormVO form) {
        if(roleService.saveFormData(form)) {
            return ResultMessage.success("添加角色信息成功!");
        }else{
            return ResultMessage.fail("添加角色信息失败!");
        }
    }

    /**
     * @param form
     * @return
     */
    @RequiresPermissions("sys:role:edit")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResultMessage update(RoleFormVO form) {
        if(roleService.saveFormData(form)) {
            return ResultMessage.success("角色信息更新成功!");
        }else{
            return ResultMessage.fail("角色信息更新失败!");
        }
    }

    @RequiresPermissions("sys:role:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    public ResultMessage remove(long id){
        if(roleService.deleteById(id)) {
            return ResultMessage.success("角色物理删除成功！");
        } else {
            return ResultMessage.fail("角色物理删除失败！");
        }
    }

    @RequiresPermissions("sys:role:remove")
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    public ResultMessage removeFlag(long id){
        if(roleService.deleteById(id)) {
            return ResultMessage.success("角色逻辑删除成功！");
        } else {
            return ResultMessage.fail("角色逻辑删除失败！");
        }
    }

}